package com.cxyzy.videoviewdemo

import android.net.Uri
import android.os.Bundle
import android.widget.MediaController
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        playFromLocal()
    }

    /**
     * 播放本地视频
     */
    private fun playFromLocal() = playVideoFromRaw(R.raw.test_video)

    /**
     * 播放raw目录下视频
     */
    private fun playVideoFromRaw(rawFileId: Int) =
        playVideo("android.resource://$packageName/$rawFileId")

    /**
     * 播放指定路径下视频，可以从本地的或网络的
     */
    private fun playVideo(filePath: String) {
        val mediaController = MediaController(this)
        videoView.setMediaController(mediaController)
        videoView.setVideoURI(Uri.parse(filePath))
        videoView.start()
    }

    /**
     * 播放网络视频
     */
    private fun playFromNetwork() =
        playVideo("https://flv2.bn.netease.com/videolib1/1811/26/OqJAZ893T/HD/OqJAZ893T-mobile.mp4")
}
